package com.example.blogother.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.blogother.mapper.ArticleMapper;
import com.example.blogother.mapper.EvaluateMapper;
import com.xpd.blog.bean.Article;
import com.xpd.blog.bean.Evaluate;
import com.xpd.blog.bean.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xpd
 * @since 2024-09-30
 */
@RestController
@RequestMapping("/evaluate")
public class EvaluateController {
    @Autowired
    EvaluateMapper evaluateMapper;
    @Autowired
    ArticleMapper articleMapper;
    //赞blog
    @RequestMapping("addEvaluate")
    public Result addEvaluate(@RequestParam int usid, @RequestParam int aid,@RequestParam int status){
        Evaluate evaluate=new Evaluate();
        evaluate.setUsid(usid);
        evaluate.setAid(aid);
        evaluate.setStatus(status);
        Article article = articleMapper.selectByAidArticle(aid);
        Integer likes = article.getLikes();
        System.out.println("status = " + status);
        System.out.println("status = " + status);
        if(status==1){
            likes+=1;
            int i = articleMapper.updateLikesByAid(aid, likes);
            int i1 = evaluateMapper.deleteByIdInt(usid, aid);
            int insert = evaluateMapper.insert(evaluate);
            return new  Result(1,"点赞成功",null);
        }else {
            LambdaQueryWrapper<Evaluate> lqw=new LambdaQueryWrapper<>();
            lqw.eq(Evaluate::getUsid,usid);
            lqw.eq(Evaluate::getAid,aid);
            lqw.eq(Evaluate::getStatus,1);
            List<Object> list = evaluateMapper.selectObjs(lqw);
            System.out.println("list = " + list);
            if(list.size()>=1){
                System.out.println("list = " + list);
                likes=likes-1;
                int i = articleMapper.updateLikesByAid(aid, likes);
            }
            int i1 = evaluateMapper.deleteByIdInt(usid, aid);
            int insert = evaluateMapper.insert(evaluate);
            return new  Result(1,"踩成功",null);
        }
    }
    //踩该blog
    @RequestMapping("delEvaluate")
    public Result delEvaluate(@RequestParam int usid, @RequestParam int aid,@RequestParam int status){
        Evaluate evaluate=new Evaluate();
        if(status==1){
            Article article = articleMapper.selectByAidArticle(aid);
            Integer likes = article.getLikes();
            likes=likes-1;
            int i1 = articleMapper.updateLikesByAid(aid, likes);
            int i = evaluateMapper.deleteByIdInt(usid, aid);
            return new  Result(1,"取消点赞成功",null);
        }else {
            int i = evaluateMapper.deleteByIdInt(usid, aid);
            return new  Result(1,"取消踩成功",null);
        }
    }
    @RequestMapping("selectEvaluate")
    public Result selectEvaluate(@RequestParam int usid, @RequestParam int aid){
        Evaluate evaluate = evaluateMapper.selectEvaluate(usid, aid);
        return new Result(1,"查询成功",evaluate);
    }
}

